package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.asn1.axdr.AxdrOctetString;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * TransResult类
 * 实现传输结果功能
 * 
 * @author wind
 * @version 1.1.1
 */
public class TransResult implements AxdrType {

    private byte[] dataCode = null;
    private long choice = -1; // _ERR_NONE_SELECTED
    private AxdrEnum dar = null;
    private AxdrOctetString retResult = null;

    public TransResult() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == 0) { // DAR
            this.dar = new AxdrEnum();
            this.dar.setConst();
            codeLength += this.dar.decode(input);
            return codeLength;
        } else if (this.choice == 1) { // RET_RESULT
            this.retResult = new AxdrOctetString();
            codeLength += this.retResult.decode(input);
            return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return this.dataCode.length;
        }

        if (this.choice == -1) { // _ERR_NONE_SELECTED
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == 1) { // RET_RESULT
            codeLength += this.retResult.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(1);
            codeLength += c.encode(output);
            return codeLength;
        } else if (this.choice == 0) { // DAR
            codeLength += this.dar.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error encoding TransResult", e);
        }
        this.dataCode = revOStream.getArray();
    }

    public long getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = -1; // _ERR_NONE_SELECTED
        this.dar = null;
        this.retResult = null;
    }

    public void setDar(AxdrEnum dar) {
        this.resetChoices();
        this.choice = 0; // DAR
        this.dar = dar;
    }

    public void setRetResult(AxdrOctetString retResult) {
        this.resetChoices();
        this.choice = 1; // RET_RESULT
        this.retResult = retResult;
    }

    @Override
    public String toString() {
        if (this.choice == 0) { // DAR
            if (this.dar != null) {
                return "choice: {dar: " + this.dar + "}";
            } else {
                return "choice is dar but dar is null";
            }
        } else if (this.choice == 1) { // RET_RESULT
            if (this.retResult != null) {
                return "choice: {retresult: " + this.retResult + "}";
            } else {
                return "choice is retResult but retResult is null";
            }
        } else {
            return "unknown";
        }
    }

    public AxdrEnum getDar() {
        return dar;
    }

    public AxdrOctetString getRetResult() {
        return retResult;
    }
}